/**
 * @ author: 汪孟苹
 */
define(["jquery", "bootstrap", "oms_config/root", "bbcare", "fu/app/comm/page/page",
	"scprit/followup/app/workBench/workBenchs.servers.js",
	"scprit/followup/app/workBench/workBenchs.ui.js"
],
	function ($, bootstrap, $root, $bb, $page, $workBenchServers, $workBenchUi) {
		var event = {}

		/**
		 * 2021 4 14
		 * 添加的的 搜索栏的 vue 实例
		 */
		var search_vm

		/**
		 * 点击"添加患者"按钮跳转到"添加患者"页面
		 */
		event.onAddPatientClick = function () {
			$bb.selectNavbar("addPatient")
		}
		/**
		 * 点击今日门诊患者跳转
		 */
		event.onBabyClick = function () {
			var rows = $(this).data("value")
			var index = $(this).index()
			var imgName = rows[index].gender == 'N' ? 'boy' : 'girl'
			var imgSrc = "page/followup/imgs/comm/" + imgName + ".png"
			var params = {
				patientSchemeId: rows[index].ptSchemeid,
				followQueueId: rows[index].followQueueId,
				patientName: rows[index].patientName,
				imgSrc: imgSrc,
				queuename: rows[index].followQueueName,
				patientId: rows[index].patientId,
				taskId: rows[index].taskId,
			}
			$bb.openPage("outPatient", params)
		}
		/**
		 * 窗口期计划随访患者/计划随访患者（tit根据flag不同有所不同）
		 */
		var windowTitText = function (flag) {
			var config = $root.init()
			var text = (flag == "2" ? config.language.workBenchfollowpatient : config.language.workBenchWindowplansfollowpatient)
			$("#windowTit").html(text)
		}
		/**
		 * 按科研编号、手机号、姓名搜索患者时，传参有所不同
		 */
		var request, lastClick
		var time = 1000
		var lockClick = function () {
			var nowClick = new Date()
			if (lastClick == null) {  //第一次
				lastClick = nowClick
				return false
			} else {
				if (Math.round((nowClick.getTime() - lastClick.getTime())) > time) {
					lastClick = nowClick
					return true
				} else {
					lastClick = nowClick
					return false
				}
			}
		}
		//
		var searchKeywordAjax = function (params) {
			var reg = /^1\d{10}$/
			if (reg.test(params.fuzzyVar)) {
				params = $.extend(params, { phoneNum: params.fuzzyVar })
			} else {
				delete params.phoneNum
			}
			if (request && request.abort && !lockClick()) {
				request.abort()
			}
			request = $workBenchServers.ajaxPatientsQueue(params, ajaxPatientsQueueCallBack)
		}
		/**
		 * 点击姓按钮进行搜索
		 */
		var onFirstNameClick = function () {
			var params = $(this).data()
			var keyword = $(this).html()
			var params1 = {
				"flag": "0",
				"page": 1,
				"rows": params.rows,
				"systemType": params.systemType,
				"patientName": keyword
			}
			windowTitText(params1.flag)
			$workBenchServers.ajaxPatientsQueue(params1, ajaxPatientsQueueCallBack)
		}
		/**
		 * 输入姓名、手机号、科研编号进行搜索
		 */
		var onKeywordClick = function () {
			var params = $(this).data()
			$(this).prev().val("")
			var keyword = $(this).prev().val()
			var params1 = {
				"flag": "0",
				"page": 1,
				"rows": params.rows,
				"systemType": params.systemType,
				"fuzzyVar": keyword
			}
			windowTitText(params1.flag)
			searchKeywordAjax(params1)
		}
		var onKeywordInput = function () {
			var params = $(this).data()
			var keyword = $(this).val()
			var flag = (keyword == "" ? "0" : "2")
			var params1 = {
				"flag": flag,
				"page": 1,
				"rows": params.rows,
				"systemType": params.systemType,
				"fuzzyVar": keyword
			}
			windowTitText(params1.flag)
			searchKeywordAjax(params1)
		}
		/**
		 * 改变队列进行搜索
		 */
		var onQueueChange = function () {
			var params = $(this).data()
			var queueId = $(this).val()
			var params1 = {
				"flag": "2",
				"page": 1,
				"rows": params.rows,
				"systemType": params.systemType,
				"queueId": queueId
			}
			windowTitText(params1.flag)
			$workBenchServers.ajaxPatientsQueue(params1, ajaxPatientsQueueCallBack)
		}
		/**
		 * 根据是否审核进行搜索
		 */
		var onTaskAuditFlagChange = function () {
			var params = $(this).data()
			var taskAuditStatus = $(this).val()
			var params1 = {
				"flag": "2",
				"page": 1,
				"rows": params.rows,
				"systemType": params.systemType,
				"taskAuditStatus": taskAuditStatus
			}
			windowTitText(params1.flag)
			$workBenchServers.ajaxPatientsQueue(params1, ajaxPatientsQueueCallBack)
		}

		/**
		 * 根据分级进行搜索
		 */
		var onScaleAssessStatus = function () {
			var params = $(this).data()
			var scaleAssessStatus = $(this).val()
			var params1 = {
				"flag": "2",
				"page": 1,
				"rows": params.rows,
				"systemType": params.systemType,
				"scaleAssessStatus": scaleAssessStatus
			}
			windowTitText(params1.flag)
			$workBenchServers.ajaxPatientsQueue(params1, ajaxPatientsQueueCallBack)
		}

		/**
		 * 点击随访患者卡片进入详情页
		 */
		var onBabyCardClick = function () {
			var followQueueId = $(this).attr("followQueueId")
			var patientSchemeId = $(this).attr("patientSchemeId")
			var patientId = $(this).attr("patientId")
			var imgSrc = $(this).find(".clickbabycard-icon").find("img").attr("src")
			var patientName = $(this).find(".clickbabycard-name").text()
			var queuename = $(this).find(".clickbabycard-queuename").text()
			var params = {
				patientSchemeId: patientSchemeId,
				followQueueId: followQueueId,
				patientName: patientName,
				imgSrc: imgSrc,
				queuename: queuename,
				patientId: patientId
			}
			$bb.openPage("outPatient", params)
		}

		/**
		 * 请求窗口期计划随访患者成功后的回调函数
		 */
		var ajaxPatientsQueueCallBack = function (data, params) {

			if (data.rows && data.rows.length > 0) {
				var pagesAll = Math.ceil(data.total / params.rows)
				if (data.FirstName) {
					if ($("#windowFirstName > button").length == 0) {
						$workBenchUi.firstNameInit({
							id: "windowFirstName",
							list: data.FirstName,
							rows: params.rows
						})
					}
					/* 姓查询 */
					$("#windowFirstName button").unbind("click")
					$("#windowFirstName button").data(params)
					$("#windowFirstName button").click(onFirstNameClick)
				}
				$workBenchUi.babyListInit({
					id: "windowBabyList",
					list: data.rows,
					rows: params.rows
				})
				// 初始化所有工具提示
				$(function () {
					$("[data-toggle='tooltip']").tooltip()
				})

				/**
				 * 2021 4 14
				 * 添加了获取当前的搜索栏信息
				 *
				 */
				if (search_vm) {
					var new_params = search_vm.getParams()
					new_params.page = params.page ? params.page : 1
					params = new_params
				}

				/* 翻页 */
				$page.turnInit({
					id: "windowBottomPage",
					pagesAll: pagesAll,
					page: params.page
				}, $workBenchServers.ajaxPatientsQueue, params, ajaxPatientsQueueCallBack)
				/* 点击随访患者卡片进入详情页 */
				$("#windowBabyList .clickbabycard").unbind("click")
				$("#windowBabyList .clickbabycard").click(onBabyCardClick)
			} else {
				$("#windowBabyList").html('<div class="col-sm-12 col-md-12 babycard-null">暂无数据..</div>')
				$("#windowBottomPage").html("")
			}

			/* 姓名、科研编号、手机号码查询 */
			$("#windowKeyword span").unbind("click")
			$("#windowKeyword span").data(params)
			$("#windowKeyword span").click(onKeywordClick)
			$("#windowKeyword input").unbind("input propertychange")
			$("#windowKeyword input").data(params)
			$("#windowKeyword input").bind("input propertychange", onKeywordInput)

			/* 根据队列名称查询 */
			$("#selectQueue").unbind("change")
			$("#selectQueue").data(params)
			$("#selectQueue").change(onQueueChange)



			/* 根据队列名称查询 */
			$("#taskAuditFlag").unbind("change")
			$("#taskAuditFlag").data(params)
			$("#taskAuditFlag").change(onTaskAuditFlagChange)


			/* 根据分级查询 */
			$("#scaleAssessStatus").unbind("change")
			$("#scaleAssessStatus").data(params)
			$("#scaleAssessStatus").change(onScaleAssessStatus)


		}
		event.ajaxPatientsQueueCallBack = function (data, params) {
			ajaxPatientsQueueCallBack(data, params)
		}


		/**
		 * 2021 4 14
		 * 初始化 Vue
		 */
		event.initVue = function () {
			document.getElementById('root').style.display = 'block'
			search_vm = new Vue({
				el: '#root',
				data: {
					keyword: '',
					queue: '全部队列',
					queueList: [],
					status: '全部状态',
					statusList: [],
					scaleAssessStatus: '高危',
					daterange: null,
					loading: false,
					showdaterange: false,
					flag: '0'			// 已知 0 为条件搜索 2为全部搜索（当有任何一个条件时使用 2）
				},
				created: function () {
					this.getQueue()
				},
				watch: {
					status: function (val) {
						this.showdaterange = val == 5
						if (val != 5) {
							this.daterange = null
						}
					}
				},
				methods: {
					/**
					 * 获取队列
					 */
					getQueue: function () {
						var that = this
						$workBenchServers.ajaxQueue(function (list) {
							that.queueList = list
						})

						// let param = {
						// 	data: {
						// 		accountId: JSON.parse($bb.getSessionStorage("account")).id
						// 	}
						// }
						// $workBenchServers.getReadWriteRole(param)
					},
					/**
					 * 搜索列表
					 */
					handleSearch: function () {

						this.flag = '2'
						var params = this.filter({
							fuzzyVar: this.keyword,
							queueId: this.queue,
							taskAuditStatus: this.status,
							scaleAssessStatus: this.scaleAssessStatus,
							appointTimeStart: this.getDate(this.daterange, 'start'),
							appointTimeEnd: this.getDate(this.daterange, 'end'),
							systemType: '00',
							flag: this.flag,
							page: 1,
							rows: 16,
						})

						// console.log("sadsadadas",params)
						this.loading = true

						var that = this
						$workBenchServers.ajaxPatientsQueue(params, function (param1, param2) {
							that.loading = false
							ajaxPatientsQueueCallBack(param1, param2)
						})
					},
					/**
					 * 获取并格式化时间至字符串
					 */
					getDate: function (date, type) {
						if (!date) return null
						if (type == 'start') {
							return date[0].getFullYear() + '-' + (date[0].getMonth() + 1) + '-' + date[0].getDate()
						} else {
							return date[1].getFullYear() + '-' + (date[1].getMonth() + 1) + '-' + date[1].getDate()
						}
					},
					getParams: function () {
						return this.filter({
							fuzzyVar: this.keyword,
							queueId: this.queue,
							taskAuditStatus: this.status,
							scaleAssessStatus: this.scaleAssessStatus,
							appointTimeStart: this.getDate(this.daterange, 'start'),
							appointTimeEnd: this.getDate(this.daterange, 'end'),
							systemType: '00',
							flag: this.flag,
							page: 1,
							rows: 16,
						})
					},
					filter: function (params) {

						for (var i in params) {
							if (i != "scaleAssessStatus"){
								if (!params[i] || params[i] == 1 || params[i] == '全部队列' || params[i] == '全部状态') {
									delete params[i]
								}
							}
						}
						return params
					}
				}
			})
		}

		return event
	})
